﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win.UltraWinGrid;

namespace BYManageInfoSystem
{
    public partial class Frm_BackQingDan : DockContent, UltraGridInterface, GridSinkInterface
    {
        private GridInterface gif = new GridInterfaceImpl();
        private DGBY.BLL.BYBackQingDanTab m_bllBYBackQingDanTab = new DGBY.BLL.BYBackQingDanTab();
        private DGBY.BLL.YSMethodTab m_bllYSMethodTab = new DGBY.BLL.YSMethodTab();
        private int m_nDelIndex = -1;
        private bool m_bInsertFlag = false;
        public Frm_BackQingDan()
        {
            InitializeComponent();
        }
        public void ultraGrid1_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
            gif.ultraGrid1_BeforeRowsDeleted(sender, e);
        }

        private void 增加一行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KeyEventArgs kea = new KeyEventArgs(Keys.Enter | Keys.Control);
            gif.ultraGrid1_KeyDown(sender, kea);
        }

        private void 删除一行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gif.DelUltraGridRow();
        }

        public void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            gif.ultraGrid1_BeforeRowUpdate(sender, e);

        }
        public void ultraGrid1_AfterRowInsert(object sender, RowEventArgs e)
        {
            gif.ultraGrid1_AfterRowInsert(sender, e);
        }
        public void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            gif.ultraGrid1_KeyDown(sender, e);
        }
        public void SearchGoodDetails()
        {
            return;
        }
        private void Frm_BackQingDan_Load(object sender, EventArgs e)
        {

            gif.SetGridSink(this);
            gif.SetUltraGrid1(this.ultraGrid1);
        }
        private void ShowAllRecord(string strWhere)
        {
            DataSet ds = m_bllBYBackQingDanTab.GetList(strWhere);
            ultraGrid1.DataSource = null;

            DataSet dsTemp = m_bllYSMethodTab.GetList("");

            this.ultraDropDownComp.DataSource = dsTemp.Tables[0];
            this.ultraDropDownComp.DisplayMember = "运输方式";
            this.ultraDropDownComp.ValueMember = "YSMethodTabID";
            for (int i = 0; i < this.ultraDropDownComp.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownComp.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownComp.DisplayLayout.Bands[0].Columns["运输方式"].Hidden = false;





            ultraGrid1.DataSource = ds.Tables[0];
            ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["运输方式"].ValueList = this.ultraDropDownComp;




        }
        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecord("");
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);

        }




        public bool AddGoodsDetail(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                DGBY.Model.BYBackQingDanTab modelBYBackQingDanTab = new DGBY.Model.BYBackQingDanTab();


                modelBYBackQingDanTab.BYBackQingDanName = CurrentUGR.Cells["出口方式"].Value.ToString();
                modelBYBackQingDanTab.IsTidan = Convert.ToBoolean(CurrentUGR.Cells["提单"].Value.ToString());
                modelBYBackQingDanTab.IsWTS = Convert.ToBoolean(CurrentUGR.Cells["委托书"].Value.ToString());
                modelBYBackQingDanTab.IsFXT = Convert.ToBoolean(CurrentUGR.Cells["放行条"].Value.ToString());
                modelBYBackQingDanTab.IsRCD = Convert.ToBoolean(CurrentUGR.Cells["入仓单"].Value.ToString());
                modelBYBackQingDanTab.IsLLD = Convert.ToBoolean(CurrentUGR.Cells["六联单"].Value.ToString());
                modelBYBackQingDanTab.IsTYD = Convert.ToBoolean(CurrentUGR.Cells["托运单"].Value.ToString());

                modelBYBackQingDanTab.YSMethodTabID = Convert.ToInt32(CurrentUGR.Cells["运输方式"].Value.ToString(), 10);

                int nId = m_bllBYBackQingDanTab.Add(modelBYBackQingDanTab);

                /****************************************************************/
                //k005 BYBackQingDanTab表增加一个序号字段  2012-08-23   
                modelBYBackQingDanTab = m_bllBYBackQingDanTab.GetModel(nId);
                modelBYBackQingDanTab.BYBackQingDanTabIndex = modelBYBackQingDanTab.BYBackQingDanTabID;
                m_bllBYBackQingDanTab.Update(modelBYBackQingDanTab);
                /****************************************************************/

                Frm_Main.Log2DB(this.Text, "添加", modelBYBackQingDanTab.BYBackQingDanName);
                ShowAllRecord("BYBackQingDanTabid = '" + nId.ToString() + "'");

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        public void InitNewAddRowValue(int nIndex)
        {
            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            CurrentUGR.Cells["运输方式"].Value = 1;
            CurrentUGR.Cells["出口方式"].Value = "";
            CurrentUGR.Cells["提单"].Value = 0;
            CurrentUGR.Cells["委托书"].Value = 0;
            CurrentUGR.Cells["放行条"].Value = 0;
            CurrentUGR.Cells["入仓单"].Value = 0;
            CurrentUGR.Cells["六联单"].Value = 0;
            CurrentUGR.Cells["托运单"].Value = 0;


            gif.AfterInitNewAddRowValue(nIndex);

        }

        public bool ModifyGoodsDetail(int nRowIndex)
        {

            try
            {
 
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.BYBackQingDanTab modelBYBackQingDanTab = m_bllBYBackQingDanTab.GetModel(nSelectFinRecvMoneyDetailTab_NewID);


                modelBYBackQingDanTab.BYBackQingDanName = CurrentUGR.Cells["出口方式"].Value.ToString();
                modelBYBackQingDanTab.IsTidan = Convert.ToBoolean(CurrentUGR.Cells["提单"].Value.ToString());
                modelBYBackQingDanTab.IsWTS = Convert.ToBoolean(CurrentUGR.Cells["委托书"].Value.ToString());
                modelBYBackQingDanTab.IsFXT = Convert.ToBoolean(CurrentUGR.Cells["放行条"].Value.ToString());
                modelBYBackQingDanTab.IsRCD = Convert.ToBoolean(CurrentUGR.Cells["入仓单"].Value.ToString());
                modelBYBackQingDanTab.IsLLD = Convert.ToBoolean(CurrentUGR.Cells["六联单"].Value.ToString());
                modelBYBackQingDanTab.IsTYD = Convert.ToBoolean(CurrentUGR.Cells["托运单"].Value.ToString());
                modelBYBackQingDanTab.BYBackQingDanTabIndex = Convert.ToInt32(CurrentUGR.Cells["序号"].Value.ToString(), 10);

                string strBGH = CurrentUGR.Cells["运输方式"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                          modelBYBackQingDanTab.YSMethodTabID = Convert.ToInt32(CurrentUGR.Cells["运输方式"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllYSMethodTab.GetList("YSMethodName = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                         modelBYBackQingDanTab.YSMethodTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["YSMethodTabID"].ToString(), 10);
                    }
                }

                m_bllBYBackQingDanTab.Update(modelBYBackQingDanTab);
                Frm_Main.Log2DB(this.Text, "修改", modelBYBackQingDanTab.BYBackQingDanName);
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        public bool DeleteGoodsDetail(int nDeleteID)
        {
            bool bRet = false;

            DGBY.Model.BYBackQingDanTab modelBYBackQingDanTabOld = m_bllBYBackQingDanTab.GetModel(nDeleteID);

            if (modelBYBackQingDanTabOld == null)
            {
                MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return bRet;
            }

            if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                try
                {

                    m_bllBYBackQingDanTab.Delete(nDeleteID);



                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    bRet = true;
                }
            }
            else
            {
                bRet = true;
            }

            return bRet;
        }


        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

      




    }
}
